package com.miracle.android_voip_simple_demo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/*
* 此版本为测试版本，使用15分钟后自动退出
*/
public class LoginActivity extends AppCompatActivity {
    private final String TAG = "LoginActivity";
    private EditText et_ip, et_port, et_userid;
    private Button btn_login;

    private String mServerIP = "203.76.212.115";
    private int mUserId = 1;
    private int mServerPort = 12345;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // 读取登陆配置表
        readLoginDate();
        initView();
    }
    //填写转发服务器ip、port、userid
    //点击登录后进入登录状态，注意，和他人用此Demo开启会议需要都进入登录状态；
    private void initView() {
        et_ip = f(R.id.et_ip);  et_ip.setText(mServerIP);
        et_port = f(R.id.et_port); et_port.setText(String.valueOf(mServerPort));
        et_userid = f(R.id.et_userid); et_userid.setText(String.valueOf(mUserId));
        btn_login = f(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_login.setEnabled(false);
                mServerIP = et_ip.getText().toString();
                mServerPort = Integer.valueOf(et_port.getText().toString());
                mUserId = Integer.valueOf(et_userid.getText().toString());

                int appKey = 160863798;
                int appSecrect = 1515381012;
                TucodeC appCore = (TucodeC)getApplication();
                //登录转发服务器
                int result = -1;
                result = appCore.getSingleVoipInstance().loginRelayServer(mServerIP, (short)mServerPort, mUserId,0,appKey,appSecrect);
                Log.i("voip_android", String.valueOf(result));
                btn_login.setEnabled(true);
                if (result != 1){
                    Toast.makeText(appCore,"登录失败",Toast.LENGTH_SHORT).show();
                    return;
                }
                saveLoginData();

                Intent intent = new Intent(LoginActivity.this, PrepareActivity.class);
                intent.putExtra("ip", et_ip.getText().toString());
                intent.putExtra("port", Integer.valueOf(et_port.getText().toString()));
                intent.putExtra("userid", Integer.valueOf(et_userid.getText().toString()));
                startActivity(intent);

            }
        });
    }

    private <T extends View> T f(int viewId) {
        return (T)findViewById(viewId);
    }


    // 读取登陆数据
    private void readLoginDate() {
        SharedPreferences preferences = getSharedPreferences("LoginInfo", 0);
        mServerIP = preferences.getString("ServerIP", mServerIP);
        mUserId = preferences.getInt("UserId", mUserId);
        mServerPort = preferences.getInt("ServerPort", mServerPort);

    }

    // 保存登陆相关数据
    private void saveLoginData() {
        SharedPreferences preferences = getSharedPreferences("LoginInfo", 0);
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putString("ServerIP", mServerIP);
        preferencesEditor.putInt("UserId", mUserId);
        preferencesEditor.putInt("ServerPort", mServerPort);
        Boolean result = preferencesEditor.commit();
        Log.d(TAG, "saveLoginData: " + result);
    }
}
